package com.lsh.respsitory;

import com.lsh.entity.SpringScheduledCron;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：LiuShihao
 * @date ：Created in 2020/8/24 4:58 下午
 * @desc ：
 */
//@CacheConfig(cacheNames = "b2c:scheduled:cache")
public interface SpringScheduledCronRepository extends JpaRepository<SpringScheduledCron, Integer> {

//    @Cacheable
    SpringScheduledCron findByCronKey(String cronKey);

    /**
     * 更新定时任务cron表达式
     *
     * @param newCron
     * @param cronKey
     * @return
     */
//    @CacheEvict(key = "#root.args[1]", beforeInvocation = true)
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    @Query(value = "update spring_scheduled_cron set cron_expression=?1 where cron_key=?2", nativeQuery = true)
    int updateCronExpressionByCronKey(String newCron, String cronKey);

    /**
     * 更新定时任务状态
     *
     * @param status
     * @param cronKey
     * @return
     */
//    @CacheEvict(key = "#root.args[1]", beforeInvocation = true)
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    @Query(value = "update spring_scheduled_cron set status=?1 where cron_key=?2", nativeQuery = true)
    int updateStatusByCronKey(Integer status, String cronKey);

    //根据状态查找第一个表达式
    SpringScheduledCron findFirstByStatus(Integer status);

    //根据CronKey查找表达式
    SpringScheduledCron findSpringScheduledCronByCronKey(String CronKey);
}